/*
console.c

Copyright (c) 2018 by Loganx.
*/
#include"console.h"

unsigned char con_row;
unsigned char con_col;
unsigned short *con_mem_start;
unsigned short *con_mem_end;
unsigned char con_pages;
unsigned int con_page_size;
unsigned int con_row_size;

unsigned short con_color;
unsigned char con_posx;
unsigned char con_posy;
unsigned short *con_origin;
unsigned short *con_ptr;

void ConsoleInitialize(void){
    con_row = 25;
    con_col = 80;
    con_mem_start = (unsigned short *)0xb8000;
    con_color = 0x0700;
    con_posx = 1;
    con_posy = 1;
    con_origin = con_mem_start;
    con_ptr = con_mem_start;
    return;
}

unsigned char  ConsoleSetColor(unsigned char color){
    unsigned char old;
    
    old = con_color >> 8;
    con_color = color << 8;

    return old;
}

void ConsolePutChar(unsigned char c){
    *con_ptr++ = c  + con_color;
    
    //需要更新行列号，并检查是否需要滚屏显示
    if(con_posx < con_col)
        con_posx++;
    else{
        con_posx = 1;
        if(con_posy < con_row)
            con_posy++;
        else
            ConsoleScrollDown(1);
    }

    return;
}

unsigned char ConsoleGotoxy(unsigned char x,unsigned char y){
    if(x > con_col || y > con_row)
        return 0;

    con_posx = x;
    con_posy = y;
    con_ptr = con_origin + y * con_row + x;
    return 1;
}

void ConsoleGotoNexty(void){
    con_ptr += con_col - con_posx + 1;

    con_posx = 1;
    if(con_posy < con_row)
            con_posy++;
    else
            ConsoleScrollDown(1);
    return;
}

void ConsoleClean(void){
    //清除整个显存区并回滚到原点
    while(con_ptr > con_mem_start){
        *con_ptr-- = CONSOLE_ERASE_WORD;
    }
    con_ptr = con_mem_start;
    ConsoleScrollUp(0);

    return;
}

void ConsoleScrollUp(unsigned int line){
    return;
}

void ConsoleScrollDown(unsigned int line){
    return;
}